package com.qire.manhua.model.vo;

import android.graphics.Rect;

import com.qire.common.collect.ArrayUtil;
import com.qire.common.model.entity.UserEntity;
import com.qire.manhua.model.entity.ComicBookEntity;
import com.qire.manhua.model.entity.ComicBookEntity.ChaptersInfo;
import com.qire.manhua.model.entity.ComicComment;
import com.qire.manhua.model.entity.ShareInfo;

/**
 * 获取章节API的调用结果JSON容器类：
 */
public class ChaptersResultsVO {

    public ShareInfoVO      shareInfo;          //分享信息
    public int              teenager;           //青少年模式： 1 开启 0 未开启
    public int              post_cnt;           //评论数量: 36,
    public AccountInfoVO    accountInfo;        //账户信息：当前账户的资产信息
    public ChapterLockVO    chapter_lock;       //解锁信息：正常返回该值为null,存在异常携带异常锁信息。
    public DetailInfoVO     detailInfo;         //漫画章节信息: {},
    public ExtraInfo        tj;                 //统计数据信息: {},
    public CommentVO[]      commentBest;        //最佳评论信息: [ ],
    public TaskInfo         task;               //任务数据信息: {}
    public AutoPayInfo      autoPay;            //自动购买提示信息

    public void fillComment(ChaptersInfo chaptersInfo) {
        if(ArrayUtil.isEmpty(commentBest)) {
            return;
        }

        ComicComment[] commentBests = new ComicComment[commentBest.length];

        for(int index = 0; index < commentBest.length; index++) {
            commentBests[index] = commentBest[index].toComicComment();
        }

        chaptersInfo.loadComments(commentBests);
    }

    public class ShareInfoVO {
        public String title;                  //分享标题: "最近发现一部超好看的漫画：重生豪门之强势归来",
        public String desc;                   //分享内容: "3岁父亲失踪，15岁被挑断手筋，16岁母亲被害车祸身亡，19岁被亲叔叔陷害，22岁的时候被自己的新婚...",
        public String img;                    //展示图片: "https://p.qiremanhua.com/uploads/book/201712/5a28ae1c74ed1.jpg?x-oss-process=image/resize,w_200",
        public String link;                   //跳转链接: "http://comic.qireshare.com/book/10033/377/?r_share=1"

        public ShareInfo toShareInfo() {
            return new ShareInfo(title, desc, img, link);
        }
    }

    public class AccountInfoVO {
        public String   coin_balance;           //金币余额:"2000.2",
        public String   bean_balance;           //金豆余额:"424.8",
        public int      vip_type;               //0普通用户1月会员2年会员
        public int      discount;               //会员折扣:95,
    }

    public class ChapterLockVO {
        public int      is_lock;                //是否锁定: "1",
        public int      lock_state;             //锁状态 锁原因:1-未登录 2-没开自动支付 3-没开自动支付，钱不够了，要充值 4-开了自动支付，钱不够了，要充值 5-自动支付购买失败了 6-is_down模式下需要解锁直接返回
        public String   lock_info;              //简单的描述锁信息: "need login",
        public String   chapter_coin;           //章节解锁所需金币: 0,
        public String   my_chapter_coin;        //我解锁的价格: 49,
        public String   vip_chapter_coin;       //VIP的价格: 34.3,
        public String   my_coin;                //我的金币余额: 0,
        public String   my_bean;                //我的金豆余额: "70.0",
        public int      is_auto_pay;            //是否开启了自动支付   这个不用于展示  因为勾选按钮每次都要选中状态: 0
        public int      ad_unlock;              //是否可以广告解锁，需要综合其他条件判断是否显示广告解锁: 1,
        public int      ad_limit;               //广告解锁剩余次数: 9

        public void refreshLockInfo() {
            UserEntity.self.setCanAdUnlockedCount(ad_limit);
        }
    }

    public class DetailInfoVO {
        public int      id;                     //章节ID: 377,
        public int      book_id;                //漫画书ID: 10033,
        public String   check_time;             //: "2017-07-01 13:25:19",
        public String   chapter_name;           //章节名称: "第2话 重生归来",
        public String   subtitle;               //子标题: "第2话",
        public int      chapter_px;             //章节排序: 2,
        public String   book_name;              //书名: "重生豪门之强势归来",
        public String   book_unruly;            //: "https://p.qiremanhua.com/uploads/book/201712/5a28ae1c74ed1.jpg",
        public String   book_author;            //: "万画筒漫画",
        public int      end_state;              //完结信息: "0",
        public int      fee_type;               //漫画书付费类型 0全免费 1 vip免费 2章节免费 3 vip限时免费
        public int      fee_start;              //: "16",
        public int      free_num;               //免费章节数: 0,
        public int      sale_state;             //: "1",
        public String   restriction_time;       //限免时间
        public int      all_free_num;           //: 0,
        public float    all_free_mm;            //: 0,
        public int      content_imgs_count;     //全章漫画图片数量: 27,
        public int      vipFree;                //漫画章节付费类型 0不免费 1 vip免费
        public String   lave_chapter_tips;      //: "",
        public Integer  shelf;                  //是否加入书架    1 收藏 0 未收藏 因为预加载的章节是不带这个字段,默认的是0就不好判断了，所以这里用Integer
        public String[] content;                //全章漫画图片列表: [],
        public RectSize[] wh;                   //全章漫画图片宽高列表: []，为了实现图片占位使用，在图片下载时需要先占位图片高度，预先加载出弹幕后在开始下载图片

        public class RectSize {
            public int  w;
            public int  h;
        }

        public ChaptersInfo fillComic(ComicBookEntity comicBookEntity) {
            ChaptersInfo chaptersInfo = comicBookEntity.chaptersPreInstalled(id);

            comicBookEntity.setEndState(end_state == 1);

            if(shelf != null) {
                // 这个字段服务器可能会在非解锁状态下不给值，所以做了一个null判断，这样做有点不太合理。
                comicBookEntity.whetherCollect(shelf == 1);
            }

            comicBookEntity.setCoverPlotVertical(book_unruly);

            comicBookEntity.loadDiscountInfo(all_free_num, all_free_mm);

            fillComic(chaptersInfo);

            return chaptersInfo;
        }

        /**
         * 加载全集阅读章节图片信息
         * @param chaptersInfo  被填充的章节
         */
        public void fillComic(ChaptersInfo chaptersInfo) {
            if(ArrayUtil.isEmpty(content)) {
                return;
            }

            int pictureRectSize = wh != null ? wh.length : 0;
            Rect[] pictureRectArray = new Rect[pictureRectSize];
            for(int index = 0; index < wh.length; index++) {
                pictureRectArray[index] = new Rect(0, 0, wh[index].w, wh[index].h);
            }

            chaptersInfo.loadAllPictureResource(content, pictureRectArray);
        }
    }

    public class ExtraInfo {
        public int      like_count;             //章节总赞数: 221,
        public int      like;                   //我是否点赞: 0
    }

    public class TaskInfo {
        public int      task_id;                //: 100
        public int      state;                  //任务完成状态: 0,
        public int      schedule;               //任务进度: 2,
        public int      is_show;                //是否显示消息提示，同一本漫画 只会在第一次阅读章节的时候显示为1: 0
    }

    public class AutoPayInfo {
        public String chapter_coin;           //章节金额: "46.6",
        public String unlock_msg;             //弹出的提示文字: "解锁成功！"
        public String unlock_bean;            //解锁本章用了的金豆
        public String unlock_coin;            //解锁本章用了的金币
    }

}
